package cn.wyj.org.controller;

import cn.wyj.basic.util.ExcelUtil;
import cn.wyj.org.domain.ShopAuditLog;
import cn.wyj.org.service.IShopService;
import cn.wyj.org.domain.Shop;
import cn.wyj.org.query.ShopQuery;
import cn.wyj.basic.util.AjaxResult;
import cn.wyj.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessageRemovedException;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLOutput;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    // 创建店铺入驻接口
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        shopService.settlement(shop);
        return AjaxResult.me().success();
    }


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryByPage(query);
    }

    /**
     * 店铺审核通过
     */

    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog) throws MessageRemovedException {
        shopService.auditPass(shopAuditLog);
        return AjaxResult.me().success();
    }
    /**
     * 店铺激活
     */
    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id){
        shopService.active(id);
        return AjaxResult.me().success();
    }

    /**
     *  店铺审核驳回
     */
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog){
        shopService.auditReject(shopAuditLog);
        return AjaxResult.me().success();
    }

    /**
     * 驳回再申请
     */
    @PostMapping("/settlement/again")
    public AjaxResult auditAgain(@RequestBody Shop shop){
        shopService.auditAgain(shop);
        return AjaxResult.me().success();
    }

    /**
     * 导出
     */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse res){
        List<Shop> all = shopService.findAll();
        ExcelUtil.exportExcel(all, "", "sheet1", Shop.class, "店铺列表.xlsx", res);

    }

    /**
     * 导入
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> all = shopService.findAll();
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 2, Shop.class);
        shops.forEach(System.out::println);
    }
    /**
     * 导入
     */
    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        return shopService.echarts();
    }
}
